package com.crack.nga2.volley;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.crack.nga2.entity.MessageContent;
import com.crack.nga2.utils.GlobalConst;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短消息详细内容请求
 * Created by Administrator on 2016/1/6.
 */
public class MessageContentRequest extends Request<List<MessageContent>> {
    private Response.Listener<List<MessageContent>> listener;

    public MessageContentRequest(int method, String url, Response.Listener<List<MessageContent>> listener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = listener;
    }


    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headFields = new HashMap<>();
        headFields.put("Cookie", GlobalConst.Cookie);
        return headFields;
    }

    @Override
    protected Response<List<MessageContent>> parseNetworkResponse(NetworkResponse response) {
        String json;
        try {
            json = new String(response.data, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Response.error(new VolleyError(e.getMessage()));
        }
        List<MessageContent> list = new ArrayList<>();
        try {
            //去除无用数据
            json = json.substring(json.indexOf("{") + 1, json.lastIndexOf("}"));
            json = json.substring(json.indexOf("{") + 1, json.lastIndexOf("}"));
            json = json.substring(json.indexOf("{"));
            JSONObject dataObject = new JSONObject(json);
            String allUsers = dataObject.getString("allUsers");
            String[] users = allUsers.split("\\s");
            Map<String, String> userMap = new HashMap<>();
            //获取参与回话的所有用户id以及对应的名字
            for (int i = 0; i < users.length - 1; i += 2) {
                userMap.put(users[i], users[i + 1]);
            }
            Gson gson = new Gson();
            for (int index = 0; index < 20; index++) {
                JSONObject contentObject = dataObject.getJSONObject(String.valueOf(index++));
                MessageContent content = gson.fromJson(contentObject.toString(), MessageContent.class);
                if (content.getFrom() == 0) {
                    content.setUsername("#SYSTEM#");
                } else {
                    String username = userMap.get(String.valueOf(content.getFrom()));
                    content.setUsername(username);
                }
                String contentStr = content.getContent();
                contentStr = contentStr.replaceAll("\\[quote\\]", "\"");
                contentStr = contentStr.replaceAll("\\[/quote\\]", "\"");
                Log.i("message", contentStr);
                content.setContent(contentStr);
                list.add(content);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return Response.success(list, HttpHeaderParser.parseCacheHeaders(response));
    }

    @Override
    protected void deliverResponse(List<MessageContent> response) {
        if (listener != null) {
            listener.onResponse(response);
        }
    }
}
